package gapidt.analysis.detectors;

import java.util.ArrayList;
import java.util.List;

import gapidt.analysis.APIDesignAnalysisUtils;
import gapidt.analysis.APIDesignAnalyzer;
import gapidt.analysis.ASTUtils;
import gapidt.analysis.DesignProblem;
import gapidt.analysis.IDesignProblemsDetector;
import gapidt.analysis.designproblems.AmbiguousMethodName;
import gapidt.analysis.designproblems.AmbiguousParameterName;
import gapidt.nlp.SemanticAnalyzer;
import gapidt.plugin.AnalysisResultsManager;

import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;

public class AmbiguousNamesDetector extends IDesignProblemsDetector<IMethod> {

	public AmbiguousNamesDetector(){
		super(IMethod.class);
	}
	
	@Override
	public List<DesignProblem<?>> detectProblems(IMethod method){
		
		List<DesignProblem<?>> designProblems = new ArrayList<DesignProblem<?>>();
		
		if(!APIDesignAnalysisUtils.isServiceMethod(method)){
			return designProblems;
		}
		
		//Detect ambiguous method name
		if(SemanticAnalyzer.isAmbiguousActionName(method.getElementName().toString())) {
			designProblems.add(new AmbiguousMethodName(method));
			AnalysisResultsManager.getInstance().addDesigProblemCount(getClass().getSimpleName());
		}
		
		//Detect ambiguous parameter names
		MethodDeclaration methodDeclaration = ASTUtils.parse(method);
		
		for(Object item : methodDeclaration.parameters()){
			SingleVariableDeclaration parameter = (SingleVariableDeclaration) item;
			
			if(SemanticAnalyzer.isAmbiguousNounName(parameter.getName().toString())){
				designProblems.add(new AmbiguousParameterName(method, parameter.getName().toString()));
				AnalysisResultsManager.getInstance().addDesigProblemCount(getClass().getSimpleName());
			}
		}
		
		return designProblems;
	}
	
}
